----------------------------------------------------------------------------------
-- Company: UM EECS 452
-- Engineer: Chih-Wei Wang
-- 
-- Create Date:    16:35:23 10/07/2006 
-- Design Name: 
-- Module Name:    7seg_LED_display - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

---- Uncomment the following library declaration if instantiating
---- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity SSD02 is
    Port ( ssd0 : in  STD_LOGIC_VECTOR (3 downto 0);
           ssd1 : in  STD_LOGIC_VECTOR (3 downto 0);
           ssd2 : in  STD_LOGIC_VECTOR (3 downto 0);
           ssd3 : in  STD_LOGIC_VECTOR (3 downto 0);
           ssd : out  STD_LOGIC_VECTOR (7 downto 0);
			  dp : in STD_LOGIC_VECTOR (3 downto 0);
			  sel : in STD_LOGIC_VECTOR (3 downto 0);
           an : inout  STD_LOGIC_VECTOR (3 downto 0);
           clk : in  STD_LOGIC);
end SSD02;

architecture Behavioral of SSD02 is

constant num0  : STD_LOGIC_VECTOR(6 downto 0) := "0000001"; -- 0
constant num1  : STD_LOGIC_VECTOR(6 downto 0) := "1001111"; -- 1
constant num2  : STD_LOGIC_VECTOR(6 downto 0) := "0010010"; -- 2
constant num3  : STD_LOGIC_VECTOR(6 downto 0) := "0000110"; -- 3
constant num4  : STD_LOGIC_VECTOR(6 downto 0) := "1001100"; -- 4
constant num5  : STD_LOGIC_VECTOR(6 downto 0) := "0100100"; -- 5
constant num6  : STD_LOGIC_VECTOR(6 downto 0) := "0100000"; -- 6
constant num7  : STD_LOGIC_VECTOR(6 downto 0) := "0001111"; -- 7
constant num8  : STD_LOGIC_VECTOR(6 downto 0) := "0000000"; -- 8
constant num9  : STD_LOGIC_VECTOR(6 downto 0) := "0001100"; -- 9
constant num10 : STD_LOGIC_VECTOR(6 downto 0) := "0001000"; -- A
constant num11 : STD_LOGIC_VECTOR(6 downto 0) := "1100000"; -- B
constant num12 : STD_LOGIC_VECTOR(6 downto 0) := "0110001"; -- C
constant num13 : STD_LOGIC_VECTOR(6 downto 0) := "1000010"; -- D
constant num14 : STD_LOGIC_VECTOR(6 downto 0) := "0110000"; -- E
constant num15 : STD_LOGIC_VECTOR(6 downto 0) := "0111000"; -- F

-- modified to spell out pimp. modify back for debugging purposes!
constant p 	 	: STD_LOGIC_VECTOR(6 downto 0) := "0011000"; -- P
constant i		: STD_LOGIC_VECTOR(6 downto 0) := "1001111"; -- I
constant m		: STD_LOGIC_VECTOR(6 downto 0) := "0001001"; -- M

signal ssd_0 : std_logic_vector(7 downto 0);
signal ssd_1 : std_logic_vector(7 downto 0);
signal ssd_2 : std_logic_vector(7 downto 0);
signal ssd_3 : std_logic_vector(7 downto 0);
signal ctr: std_logic_vector(12 downto 0);

begin
  
  SSD_select : process(sel)
  begin
    if sel(0) = '0' then
	   ssd_0(6 downto 0) <= "1111111";
	 else	 
-- modified to spell out pimp. modify back for debugging purposes!
	   case ssd0 is
--		  when "0000" => ssd_0(6 downto 0) <= p;--num0;
--		  when "0001" => ssd_0(6 downto 0) <= i; --num1;
--		  when "0010" => ssd_0(6 downto 0) <= m; --num2;
		  when "0000" => ssd_0(6 downto 0) <= num0;
		  when "0001" => ssd_0(6 downto 0) <= num1;
		  when "0010" => ssd_0(6 downto 0) <= num2;
		  when "0011" => ssd_0(6 downto 0) <= num3;
		  when "0100" => ssd_0(6 downto 0) <= num4;
		  when "0101" => ssd_0(6 downto 0) <= num5;
		  when "0110" => ssd_0(6 downto 0) <= num6;
		  when "0111" => ssd_0(6 downto 0) <= num7;
		  when "1000" => ssd_0(6 downto 0) <= num8;
		  when "1001" => ssd_0(6 downto 0) <= num9;
		  when "1010" => ssd_0(6 downto 0) <= num10;
		  when "1011" => ssd_0(6 downto 0) <= num11;
		  when "1100" => ssd_0(6 downto 0) <= num12;
		  when "1101" => ssd_0(6 downto 0) <= num13;
		  when "1110" => ssd_0(6 downto 0) <= num14;
		  when others => ssd_0(6 downto 0) <= num15;
		end case;
	 end if;
	 if sel(1) = '0' then
	   ssd_1(6 downto 0) <= "1111111";
	 else
	   case ssd1 is
--		  when "0000" => ssd_1(6 downto 0) <= p; --num0;
--		  when "0001" => ssd_1(6 downto 0) <= i; --num1;
--		  when "0010" => ssd_1(6 downto 0) <= m; --num2;
		  when "0000" => ssd_0(6 downto 0) <= num0;
		  when "0001" => ssd_0(6 downto 0) <= num1;
		  when "0010" => ssd_0(6 downto 0) <= num2;
		  when "0011" => ssd_1(6 downto 0) <= num3;
		  when "0100" => ssd_1(6 downto 0) <= num4;
		  when "0101" => ssd_1(6 downto 0) <= num5;
		  when "0110" => ssd_1(6 downto 0) <= num6;
		  when "0111" => ssd_1(6 downto 0) <= num7;
		  when "1000" => ssd_1(6 downto 0) <= num8;
		  when "1001" => ssd_1(6 downto 0) <= num9;
		  when "1010" => ssd_1(6 downto 0) <= num10;
		  when "1011" => ssd_1(6 downto 0) <= num11;
		  when "1100" => ssd_1(6 downto 0) <= num12;
		  when "1101" => ssd_1(6 downto 0) <= num13;
		  when "1110" => ssd_1(6 downto 0) <= num14;
		  when others => ssd_1(6 downto 0) <= num15;
		end case;
    end if;
    if sel(2) = '0' then
      ssd_2(6 downto 0) <= "1111111";
    else
      case ssd2 is
--		  when "0000" => ssd_2(6 downto 0) <= p; --num0;
--		  when "0001" => ssd_2(6 downto 0) <= i; --num1;
--		  when "0010" => ssd_2(6 downto 0) <= m; --num2;
		  when "0000" => ssd_0(6 downto 0) <= num0;
		  when "0001" => ssd_0(6 downto 0) <= num1;
		  when "0010" => ssd_0(6 downto 0) <= num2;
		  when "0011" => ssd_2(6 downto 0) <= num3;
		  when "0100" => ssd_2(6 downto 0) <= num4;
		  when "0101" => ssd_2(6 downto 0) <= num5;
		  when "0110" => ssd_2(6 downto 0) <= num6;
		  when "0111" => ssd_2(6 downto 0) <= num7;
		  when "1000" => ssd_2(6 downto 0) <= num8;
		  when "1001" => ssd_2(6 downto 0) <= num9;
		  when "1010" => ssd_2(6 downto 0) <= num10;
		  when "1011" => ssd_2(6 downto 0) <= num11;
		  when "1100" => ssd_2(6 downto 0) <= num12;
		  when "1101" => ssd_2(6 downto 0) <= num13;
		  when "1110" => ssd_2(6 downto 0) <= num14;
		  when others => ssd_2(6 downto 0) <= num15;
		end case;	 
    end if;
	 if sel(3) = '0' then
	   ssd_3(6 downto 0) <= "1111111";
	 else
      case ssd3 is
--		  when "0000" => ssd_3(6 downto 0) <= p; --num0;
--		  when "0001" => ssd_3(6 downto 0) <= i; --num1;
--		  when "0010" => ssd_3(6 downto 0) <= m; --num2;
		  when "0000" => ssd_0(6 downto 0) <= num0;
		  when "0001" => ssd_0(6 downto 0) <= num1;
		  when "0010" => ssd_0(6 downto 0) <= num2;
		  when "0011" => ssd_3(6 downto 0) <= num3;
		  when "0100" => ssd_3(6 downto 0) <= num4;
		  when "0101" => ssd_3(6 downto 0) <= num5;
		  when "0110" => ssd_3(6 downto 0) <= num6;
		  when "0111" => ssd_3(6 downto 0) <= num7;
		  when "1000" => ssd_3(6 downto 0) <= num8;
		  when "1001" => ssd_3(6 downto 0) <= num9;
		  when "1010" => ssd_3(6 downto 0) <= num10;
		  when "1011" => ssd_3(6 downto 0) <= num11;
		  when "1100" => ssd_3(6 downto 0) <= num12;
		  when "1101" => ssd_3(6 downto 0) <= num13;
		  when "1110" => ssd_3(6 downto 0) <= num14;
		  when others => ssd_3(6 downto 0) <= num15;
		end case;	 
     end if;
   end process SSD_select;
	
	ssd_0(7) <= not dp(0);
	ssd_1(7) <= not dp(1);
	ssd_2(7) <= not dp(2);
	ssd_3(7) <= not dp(3);

   -- SSD main unit
   process (clk)
   begin
     if clk'event and clk = '1' then
	    if (ctr="0000000000000") then
		   if (an(3)='0') then
		     an(3) <= '1';
			  ssd <= ssd_2; -- led2
			  an(2) <= '0';
		   elsif (an(2)='0') then
			  an(2) <= '1';
           ssd <= ssd_1;  -- led1
           an(1) <= '0';
         elsif (an(1)='0') then
           an(1) <= '1';
           ssd <= ssd_0;  -- led0
           an(0) <= '0';
         elsif (an(0)='0') then
           an(0) <= '1';
           ssd <= ssd_3; -- led3
           an(3) <= '0';  
         end if;
       end if;
       ctr <= ctr+"0000000000001";
       if (ctr > "1000000000000") then
         CTR <= "0000000000000";
       end if;			
     end if;
   end process;

end Behavioral;

